package com.yc.testupload;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import com.yc.testupload.util.EnvironmentManager;

/**
 * 数据库连接测试类
 * 用于诊断数据库连接问题
 */
public class DatabaseConnectionTest {

    public static void main(String[] args) {
        System.out.println("=== 数据库连接测试开始 ===");
        
        // 直接使用硬编码的配置进行测试
        String url = "jdbc:mysql://115.159.219.140:3306/testdidadoc2?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&connectTimeout=10000&socketTimeout=10000";
        String username = "testdidadoc2";
        String password = "PfiyxdTSAG2iPpma";
        
        System.out.println("测试配置:");
        System.out.println("URL: " + url);
        System.out.println("Username: " + username);
        System.out.println("Password: ******");
        
        Connection conn = null;
        try {
            // 加载驱动
            System.out.println("正在加载驱动...");
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("驱动加载成功");
            
            // 尝试连接
            System.out.println("正在尝试连接数据库...");
            conn = DriverManager.getConnection(url, username, password);
            System.out.println("数据库连接成功!");
            System.out.println("连接状态: " + !conn.isClosed());
            
            // 测试连接可用性
            System.out.println("测试连接可用性...");
            boolean isValid = conn.isValid(5); // 5秒超时
            System.out.println("连接是否有效: " + isValid);
            
        } catch (ClassNotFoundException e) {
            System.err.println("驱动类未找到: " + e.getMessage());
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println("数据库连接失败: " + e.getMessage());
            System.err.println("SQL状态: " + e.getSQLState());
            System.err.println("错误代码: " + e.getErrorCode());
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                    System.out.println("连接已关闭");
                } catch (SQLException e) {
                    System.err.println("关闭连接失败: " + e.getMessage());
                }
            }
        }
        
        // 也测试EnvironmentManager的配置
        System.out.println("\n=== 测试EnvironmentManager配置 ===");
        try {
            EnvironmentManager envMgr = EnvironmentManager.getInstance();
            System.out.println("当前环境: " + envMgr.getCurrentEnvironment());
            
            Properties config = envMgr.getCurrentDatabaseConfig();
            System.out.println("加载的配置项数量: " + config.size());
            System.out.println("JDBC URL: " + config.getProperty("jdbc.url"));
            System.out.println("JDBC Username: " + config.getProperty("jdbc.username"));
            
            // 尝试使用加载的配置连接
            System.out.println("\n使用加载的配置尝试连接...");
            String loadedUrl = config.getProperty("jdbc.url");
            String loadedUsername = config.getProperty("jdbc.username");
            String loadedPassword = config.getProperty("jdbc.password");
            
            Connection loadedConn = DriverManager.getConnection(loadedUrl, loadedUsername, loadedPassword);
            System.out.println("使用加载的配置连接成功!");
            loadedConn.close();
            
        } catch (Exception e) {
            System.err.println("EnvironmentManager测试失败: " + e.getMessage());
            e.printStackTrace();
        }
        
        System.out.println("\n=== 数据库连接测试结束 ===");
    }
}